package com.hunt.app.controller;

import com.hunt.controller.OptionalStockController;
import com.hunt.dao.UserInfoMapper;
import com.hunt.model.app.CollectStock;
import com.hunt.model.app.CollectStockGroupVo;
import com.hunt.model.app.RealTimeSimpleStockData;
import com.hunt.service.app.CollectStockService;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: ww
 * @Date : 2020/10/19
 */

@Api(value = "pc/app端公用的自选股功能")
@Controller
@RequestMapping(path = "/pc/app/collectStock")
public class CollectStockController {

    private static final Logger log = LoggerFactory.getLogger(OptionalStockController.class);

    @Autowired
    private CollectStockService collectStockService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @ApiOperation(value = "仅查询分组信息(不包含分组内的股票列表)", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "onlyFindCollectStockGroupList", method = RequestMethod.POST)
    public Result onlyFindCollectStockGroupList(@RequestParam(value = "userId") String userId,
                                                @RequestParam(value = "stockBase",defaultValue = "",required = false) String stockBase) {
        int i = userInfoMapper.selectCountByUserId(userId);
        if (i == 0){
            return Result.unknown_account(new ArrayList<>());
        }
        List<CollectStockGroupVo> list;
        try {
            list = collectStockService.onlyFindCollectStockGroupList(userId,stockBase);
            if (list == null || list.size() == 0){
                list = new ArrayList<>();
            }
        }catch (Exception e){
            log.info("仅查询分组信息失败,userID:{}",userId);
            e.printStackTrace();
            return Result.error();
        }
        return Result.success(list);
    }

    @ApiOperation(value = "查询某组自选股(如果groupId为空返回默认分组详情)", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findCollectStockGroupInfo", method = RequestMethod.POST)
    public Result findCollectStockGroupInfo(@RequestParam(value = "userId") String userId,
                                                @RequestParam(value = "groupId",defaultValue = "",required = false) String groupId) {
        int i = userInfoMapper.selectCountByUserId(userId);
        if (i == 0){
            return Result.unknown_account();
        }
        CollectStock collectStock;
        try {
            collectStock = collectStockService.findCollectStockGroupInfo(userId,groupId);
        }catch (Exception e){
            log.info("查询某组自选股失败,userID:{}",userId);
            e.printStackTrace();
            return Result.error();
        }
        return Result.success(collectStock);
    }

    @ApiOperation(value = "新增分组", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "addCollectStockGroup", method = RequestMethod.POST)
    public Result addCollectStockGroup(@RequestParam(value = "userId") String userId,
                                       @RequestParam(value = "groupName") String groupName,
                                       @RequestParam(value = "stockInfo",defaultValue = "",required = false) String stockInfo) {
        int i = userInfoMapper.selectCountByUserId(userId);
        if (i == 0){
            return Result.unknown_account();
        }
        try {
            Result result = collectStockService.addCollectStockGroup(userId, groupName, stockInfo);
            return result;
        }catch (Exception e){
            log.info("新增分组失败,userID:{}",userId);
            e.printStackTrace();
            return Result.error(null);
        }
    }

    @ApiOperation(value = "新增自选股到分组", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "addCollectStock2Group", method = RequestMethod.POST)
    public Result addCollectStock2Group(@RequestParam(value = "userId") String userId,
                                        @RequestParam(value = "groupId",defaultValue = "",required = false) String groupId,
                                        @RequestParam(value = "stockInfo") String stockInfo) {
        int i = userInfoMapper.selectCountByUserId(userId);
        if (i == 0){
            return Result.unknown_account();
        }
        boolean b;
        try {
            b = collectStockService.addCollectStock2Group(userId,groupId,stockInfo);
        }catch (Exception e){
            log.info("分组新增自选股失败");
            e.printStackTrace();
            return Result.error();
        }
        if (b){
            return Result.success();
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value = "修改或排序或删除分组内的自选股(置顶/置底/删除)", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "operationWithinGroup", method = RequestMethod.POST)
    public Result operationWithinGroup(@RequestParam(value = "userId") String userId,
                                         @RequestParam(value = "group") String group) {
        int i = userInfoMapper.selectCountByUserId(userId);
        if (i == 0){
            return Result.unknown_account();
        }
        try {
            return collectStockService.operationWithinGroup(userId,group);
        }catch (Exception e){
            log.info("修改或删除分组内的信息失败");
            e.printStackTrace();
            return Result.error();
        }
    }



    @ApiOperation(value = "修改或排序或删除分组信息(修改/排序/删除)", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "operationGroup", method = RequestMethod.POST)
    public Result operationGroup(@RequestParam(value = "userId") String userId,
                                       @RequestParam(value = "groupList") String groupList) {
        int i = userInfoMapper.selectCountByUserId(userId);
        if (i == 0){
            return Result.unknown_account();
        }

        try {
            return collectStockService.operationGroup(userId,groupList);
        }catch (Exception e){
            log.info("修改或删除分组失败");
            e.printStackTrace();
            return Result.error(null);
        }
    }


    @ApiOperation(value = "将自选股同步到各个分组", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "syncStock2Group", method = RequestMethod.POST)
    public Result syncStock2Group(@RequestParam(value = "userId") String userId,
                                  @RequestParam(value = "stockInfo") String stockInfo,
//                                  @RequestParam(value = "groupVoList[]") List<CollectStockGroupVo> groupVoList) {
                                  @RequestParam(value = "groupIdList[]") List<String> groupIdList) {
        int i = userInfoMapper.selectCountByUserId(userId);
        if (i == 0){
            return Result.unknown_account();
        }
        boolean b;
        try {
//            b = collectStockService.syncStock2Group(userId,stockInfo,groupVoList);
            b = collectStockService.syncStock2Group(userId,stockInfo,groupIdList);
        }catch (Exception e){
            log.info("将自选股同步到各个分组失败");
            e.printStackTrace();
            return Result.error();
        }
        if (b){
            return Result.success();
        }else{
            return Result.error();
        }
    }
    //----------------------------------------------------------------------------------------------------------------------------

    @ApiOperation(value = "查询用户所有自选股信息(包含自选股分组及分组内自选股)", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "selectCollectStockDetails", method = RequestMethod.POST)
    public Result selectCollectStockDetails(@RequestParam(value = "userId") String userId) {
        int i = userInfoMapper.selectCountByUserId(userId);
        if (i == 0){
            return Result.unknown_account();
        }
        CollectStock collectStock;
        try {
            collectStock = collectStockService.selectCollectStockDetails(userId);
        }catch (Exception e){
            log.info("查询用户所有自选股信息失败");
            e.printStackTrace();
            return Result.error();
        }
        return Result.success(collectStock);
    }



    @ApiOperation(value = "将所有股票后缀.SH 转换成对应的后缀", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "updateStockSuffix", method = RequestMethod.GET)
    public Result updateStockSuffix(@RequestParam(value = "userId",required = false) String userId) {
        collectStockService.updateStockSuffix(userId);
        return Result.success();
    }
    @ApiOperation(value = "返回按规则排序的自选股列表", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getCollectStockbyhlpercent", method = RequestMethod.POST)
    public Result getCollectStockbyhlpercent(@RequestParam(value = "userId") String userId,@RequestParam(value = "groupId",defaultValue = "Default",required = false) String groupId
            ,@RequestParam(value = "sort",defaultValue = "0",required = false)@ApiParam(value = "0为正向,1为反向") String sort,
                                             @RequestParam(value = "type",defaultValue = "0",required = false)@ApiParam(value = "1为最新价,2为涨跌,3为涨跌幅,其它的为无排序") String type
                                             ) {
        List<RealTimeSimpleStockData> realTimeSimpleStockDatas  =collectStockService.getCollectStockbylimit(userId,groupId,sort,type);
        return Result.success(realTimeSimpleStockDatas);
    }
}
